<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">

        <el-form-item :label="$t('iot.processCode')" prop="checkProcessCode">
          <el-input v-model="state.queryForm.checkProcessCode" style="width:120px" clearable :placeholder="$t('iot.processCode')"/>
        </el-form-item>

        <el-form-item :label="$t('qms.checkItemCode')" prop="productionCheckCode">
          <el-input v-model="state.queryForm.productionCheckCode" style="width:120px" clearable :placeholder="$t('qms.checkItemCode')"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary"  @click="getDataList">{{$t('btn.search')}}</el-button>
          <el-button icon="refresh"  @click="resetQuery">{{$t('btn.reset')}}</el-button>
          <el-button icon="el-icon-plus"  v-auth="'platform_qmsProductionCheck_add'"  type="primary" @click="formDialogRef.openDialog() ">{{$t('operation.add')}}</el-button>
          <el-button icon="el-icon-delete" :disabled="multiple" v-auth="'platform_qmsProductionCheck_del'" type="danger" @click="handleDelete(selectObjs)">{{$t('btn.delete')}}</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="state.dataList" v-loading="state.loading" border
                :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                @selection-change="selectionChangHandle" style="width: 100%;height: 100%;"
                @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center"/>
        <el-table-column prop="checkProcessCode" :label="$t('iot.processCode')" show-overflow-tooltip/>
        <el-table-column prop="productionCheckCode" :label="$t('qms.checkItemCode')" show-overflow-tooltip/>
        <el-table-column prop="productionCheckName" :label="$t('qms.checkItemName')" show-overflow-tooltip/>
        <el-table-column prop="checkLocationCode" :label="$t('qms.checkLocationCode')" show-overflow-tooltip/>
        <el-table-column prop="checkLocationName" :label="$t('qms.checkLocationName')" show-overflow-tooltip/>
        <el-table-column prop="isEnable" :label="$t('qms.enableOrNot')" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :effect="scope.row.isEnable === 'Y' ? 'dark' : 'light'" :type="scope.row.isEnable === 'Y' ? 'success' : 'danger'" >
              {{ scope.row.isEnable}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" :label="$t('basDicSet.remark')" show-overflow-tooltip/>
        <el-table-column :label="$t('operation.Operation')" width="150">
          <template #default="scope">
            <el-button type="primary" icon="edit" v-auth="'platform_basDicSet_edit'"
                       @click="formDialogRef.openDialog(scope.row.id)"></el-button>
            <el-button  type="danger" icon="delete" @click="handleDelete([scope.row.id])"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)"/>

  </div>
</template>

<script setup lang="ts" name="systemQmsProductionCheck">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {delObjs, fetchList} from "/@/api/platform/qms/qmsProductionCheck";
import {useMessage, useMessageBox} from "/@/hooks/message";
// 定义查询字典
import {useI18n} from "vue-i18n";

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const {t} = useI18n();

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()

// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList(false)
}


// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('operation.permanentDeletion'));
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success(t('qms.Successful'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>